<?php
/*********************************************
  CPG Dragonfly™ CMS
  ********************************************
  Copyright © 2004 - 2007 by CPG-Nuke Dev Team
  http://dragonflycms.org

  Dragonfly is released under the terms and conditions
  of the GNU GPL version 2 or any later version
**********************************************/
if (!defined('DF_MODE_INSTALL') || !DF_MODE_INSTALL) exit('Installer mode is not active. Set includes/config.php define(\'DF_MODE_INSTALL\', true);');

$DF->setState(DF::BOOT_INSTALL);

$go = $_POST->uint('step') ? : 0;
if ($go <= 2) define('DF_MODE_NOSQL', 1);
if ($go <= 3) define('DF_MODE_NOCFG', 1);

define('INSTALL', true);
define('SKIP_GZIP', true);
define('SKIP_BUFFERING', true);
require_once(CORE_PATH.'cmsinit.inc');
require_once(CORE_PATH.'cpg_page.php');


$images = array();
for ($i=0; $i<6; ++$i) {
	$images[$i] = (($go == $i) ? 'box_current' : (($go > $i) ? 'checked' : 'unchecked'));
}

if ($go < 4 && isset($_COOKIE['installtest'])) { setcookie('installtest','',-1); }

$config_file = CORE_PATH.'config.php';
if (file_exists($config_file)) {
	$db = new \Poodle\SQL\Dragonfly(
		('mysql'==DB_TYPE?'mysqli':DB_TYPE),
		array(
			'host'     => $dbhost,
			'username' => $dbuname,
			'password' => $dbpass,
			'database' => $dbname,
			'charset'  => DB_CHARSET
		),
		$prefix.'_'
	);
} else {
	$adminindex = 'admin';
	$mainindex = 'index.php';
	define('CPG_DEBUG', false);
}

# Load the language
$currentlang = $tmplang = 'en';
$instlang = array();
if (isset($_GET['newlang']) && preg_match('#^[a-z]+$#', $_GET['newlang'])) {
	setcookie('installlang', $_GET['newlang']);
	$tmplang = $_GET['newlang'];
} elseif (isset($_COOKIE['installlang']) && preg_match('#^[a-z]+$#', $_COOKIE['installlang'])) {
	$tmplang = $_COOKIE['installlang'];
} else {
	$tmplang = Client::$languages;
}

if (is_array($tmplang)) {
	foreach ($tmplang as $var) {
		if (file_exists(BASEDIR."install/language/$var.php")) {
			$currentlang = $var;
			break;
		}
	}
} else if (file_exists(BASEDIR."install/language/$tmplang.php")) {
	$currentlang = $tmplang;
}
$tmplang = $currentlang;
require(BASEDIR."install/language/$currentlang.php");

$MAIN_CFG['global'] = array('language' => $currentlang, 'multilingual' => '0', 'GoogleTap' => '0', 'top' => '', 'adminmail' => '');
require_once(CORE_PATH.'functions/language.php');
// above overwrites $currentlang

if (file_exists($config_file)) {
	$current_version = check_inst($go < 3);
}

function check_inst($die=false) {
	global $db, $prefix, $dbname, $instlang;
	if (isset($db->TBL->config_custom) && $result = $db->query('SELECT cfg_value FROM '.$prefix.'_config_custom WHERE cfg_name=\'global\' AND cfg_field=\'Version_Num\'', true)) {
		list($version) = $db->sql_fetchrow($result);
		if ($die && $version == CPG_NUKE) {
			inst_header();
			echo $instlang['s1_already'];
			footer();
			exit;
		}
		return $version;
	}
	return 0;
}

function get_db_vars($db) {
	return $db->get_details();
}

function inst_header() {
	global $images, $instlang, $go, $tmplang;
	echo cpg_header($instlang['installer']).'
<script language="JavaScript" type="text/javascript" src="includes/javascript/infobox.js"></script>
<div id="infodiv" style="position:absolute; visibility:hidden; z-index:20; top:0px; left:0px;"></div><br />
<form action="?install" method="post">
<table width="100%" height="350px">
<tr><td style="min-width:200px; vertical-align:baseline; text-align:left">
<b>'.$instlang['s_progress'].'</b><br />
  <img src="images/'.$images[0].'.gif" alt="" />'.$instlang['s_license'].'<br />
  <img src="images/'.$images[1].'.gif" alt="" />'.$instlang['s_server'].'<br />
  <img src="images/'.$images[2].'.gif" alt="" />'.$instlang['s_setconfig'].'<br />
  <img src="images/'.$images[3].'.gif" alt="" />'.$instlang['s_builddb'].'<br />
  <img src="images/'.$images[4].'.gif" alt="" />'.$instlang['s_gather'].'<br />
  <img src="images/'.$images[5].'.gif" alt="" />'.$instlang['s_create'].'<br />';
	if (!$go) {
		echo '<br />'._SELECTLANGUAGE.'<br />
<select name="newlanguage" onchange="top.location.href=\'?install&amp;newlang=\'+this.options[this.selectedIndex].value" class="formfield">';
		$content = '';
		$handle = opendir(BASEDIR.'install/language');
		while ($file = readdir($handle)) {
			if (preg_match('#(.*).php#', $file, $matches)) {
				$languageslist[] = $matches[1];
			}
		}
		closedir($handle);
		sort($languageslist);
		for ($i=0; $i < sizeof($languageslist); $i++) {
			if ($languageslist[$i]!='') {
				$content .= '<option value="'.$languageslist[$i].'"';
				if ($languageslist[$i]==$tmplang) $content .= ' selected="selected"';
				$content .= '>'.ucfirst(Client::$browserlang[$languageslist[$i]])."</option>\n";
			}
		}
		echo $content.'</select>';
	}
	echo '
</td><td valign="top" align="center">';
	flush();

}
function footer() {
	echo '</td></tr></table></form>
  </td></tr></table>
</td></tr></table>
</center></body></html>';
}

function inst_help($item) {
	return '<img src="install/images/help.gif" alt="" onmouseover="tip(\''.$item.'\')" onmouseout="untip()" style="cursor: help;" />';
}

if (!$go) {
	inst_header();
	echo '<h2>'.$instlang['welcome'].'</h2>
<p style="font-size:12px">'.$instlang['info'].'</p><p style="font-size:12px">'.$instlang['click'].'</p>';
	echo '<p>';
	if (function_exists('readgzfile')) {
		echo '<textarea name="textfield" rows="15" cols="80">';
		readgzfile('install/GPL.gz');
		echo '</textarea>';
	} else {
		echo '<h2 align="center">'.$instlang['no_zlib'].'</h2>';
	}
	echo '<br clear="all" /><br />
<input type="hidden" name="step" value="'.(!empty($current_version) ? '3' : '1').'" />
<input type="submit" value="'.$instlang['agree'].'" class="formfield" /></p>';
}
elseif (isset($_SERVER['HTTP_REFERER']) && strlen($_SERVER['HTTP_REFERER']) > 0 && !stripos($_SERVER['HTTP_REFERER'], '://'.$_SERVER['HTTP_HOST'])) {
	echo 'Posting from another server is not allowed';
}
elseif (file_exists(BASEDIR."install/step$go.php")) {
	include(BASEDIR."install/step$go.php");
}
else {
	echo '<h1>'.sprintf(_ERROR_NO_EXIST, $go).'</h1>';
}
footer();
